﻿/*版权声明
 * 
 * 本 WebName:IMIS_ERP 由
 * 河北大学 信管团队 开发
 * 如有任何疑问请联系开发人员
 * 
 * 项目开发者
 * 负责人：朱胜贤
 * 成员：水亚亚、孙旭峰、张文元、项玥、吕芳、吴凡、谢静、李亚慧
 * 
 * 如有任何疑问请联系：河北大学信管团队
 * QQ：196396337
 * Email:zagelover@163.com
 * HomePage：http://sxgzs.com/
 * 
 * CopyRight (c) 河北大学信管团队 2010~2020
 */

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Data.SqlClient;
using System.Xml;

namespace IMIS_ERP.Web.Order_Info
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        //***更新时注意***
        //与Modify_Flagship.aspx很多功能一致，需要同步更新
        //***更新时注意***

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
                {
                    string oiID = Request.Params["id"];
                    ShowInfo(oiID);
                    Bind_GV();
                    Set_GV();
                    Bind_PID();
                    Bind_DisCount();
                    btnCreate.Attributes.Add("onclick", "return confirm(\"确认提交吗？提交前请保存订单的所有更改！\")");
                }
            }
        }

        private void Set_GV()
        {
            if (GV.Rows.Count > 0 && ckboiIsSendToF.Checked)
            {
                GV.Columns[14].Visible = false;
            }
        }

        /// <summary>绑定数据：折扣
        ///     <remarks>2011-01-21 河大信管团队</remarks>
        /// </summary>
        private void Bind_DisCount()
        {
            IMIS_ERP.BLL.Products_DisCount bll = new IMIS_ERP.BLL.Products_DisCount();
            string disCount = bll.GetDisCount(Session["siID"].ToString());
            if (disCount != "0" && disCount != "")
            {
                txtopDisCount.Text = disCount;
            }
        }

        /// <summary>绑定数据：产品编号
        ///     <remarks>2010-12-12 河大信管团队</remarks>
        /// </summary>
        private void Bind_PID()
        {
            IMIS_ERP.BLL.Products_Info bll = new IMIS_ERP.BLL.Products_Info();
            SqlDataReader dr = bll.GetList();
            ddlopPiID.DataSource = dr;
            ddlopPiID.DataTextField = "piID";
            ddlopPiID.DataValueField = "piName";
            ddlopPiID.DataBind();
            dr.Close();
            Bind_ddlopWallThick();
        }

        /// <summary>绑定数据：壁厚
        ///     <remarks>2010-12-12 河大信管团队</remarks>
        /// </summary>
        private void Bind_ddlopWallThick()
        {
            Bind_DisCount();
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(Server.MapPath("../Pub_Useful/Useful.xml"));
            string nonDisCount = xmlDoc.SelectSingleNode("nonDisCount").InnerText;
            if (nonDisCount.Contains(ddlopPiID.SelectedItem.Text))
            {
                txtopDisCount.Text = "1.0";
            }
            txtopPiName.Text = ddlopPiID.SelectedValue;
            IMIS_ERP.BLL.Products_Cost_Common bll = new IMIS_ERP.BLL.Products_Cost_Common();
            SqlDataReader dr = bll.GetList_WallThick(ddlopPiID.SelectedItem.Text);
            ddlopWallThick.DataSource = dr;
            ddlopWallThick.DataTextField = "pcWallThick";
            ddlopWallThick.DataValueField = "pcWallThick";
            ddlopWallThick.DataBind();
            dr.Close();
            Bind_Price();
        }

        /// <summary>绑定数据：产品价格
        ///     <remarks>2010-12-17 河大信管团队</remarks>
        /// </summary>
        private void Bind_Price()
        {
            if (ddlopWallThick.SelectedValue != "")
            {
                txtopPcID.Text = ddlopPiID.SelectedItem.Text.Trim() + "-" + ddlopWallThick.SelectedValue.Trim().Substring(0, 1) + ddlopWallThick.SelectedValue.Trim().Substring(2);
            }
            IMIS_ERP.BLL.Products_Cost bll = new IMIS_ERP.BLL.Products_Cost();
            SqlDataReader dr = bll.GetPrice(txtopPcID.Text, Session["siID"].ToString());
            if (dr.HasRows)
            {
                if (dr.Read())
                {
                    txtopPrice1.Text = dr["pcPricePerKg"].ToString();
                    txtopPrice2.Text = dr["pcPricePerSquare"].ToString();
                    txtopFigure.Text = dr["pcFigure"].ToString();
                    ViewState["pcWeightPerSquare"] = dr["pcWeightPerSquare"];
                    ViewState["pcPrice"] = dr["pcPrice"];
                    ViewState["pcFeeInstall"] = dr["pcFeeInstall"];
                    ViewState["pcFeeAgent"] = dr["pcFeeAgent"];
                    ddlopLock.Items.FindByText("是").Value = dr["pcLock"].ToString();
                    txtopFigureNum.Text = "0";
                    txtopFigureSum.Text = "0";
                }
            }
            else
            {
                IMIS_ERP.BLL.Products_Cost_Common bll1 = new IMIS_ERP.BLL.Products_Cost_Common();
                IMIS_ERP.Model.Products_Cost_Common model1 = bll1.GetModel(txtopPcID.Text);
                txtopPrice1.Text = model1.pcPricePerKg.ToString();
                txtopPrice2.Text = model1.pcPricePerSquare.ToString();
                txtopFigure.Text = model1.pcFigure.ToString();
                ViewState["pcWeightPerSquare"] = model1.pcWeightPerSquare.ToString();
                ViewState["pcPrice"] = model1.pcPrice.ToString();
                ViewState["pcFeeInstall"] = model1.pcFeeInstall.ToString();
                ViewState["pcFeeAgent"] = model1.pcFeeAgent.ToString();
                ddlopLock.Items.FindByText("是").Value = model1.pcLock.ToString();
                txtopFigureNum.Text = "0";
                txtopFigureSum.Text = "0";
            }
        }

        /// <summary>绑定数据：详细信息
        ///     <remarks>2010-12-08 河大信管团队</remarks>
        /// </summary>
        /// <param name="oiID">编号</param>
        private void ShowInfo(string oiID)
        {
            IMIS_ERP.BLL.Order_Info bll = new IMIS_ERP.BLL.Order_Info();
            IMIS_ERP.Model.Order_Info model = bll.GetModel(oiID);
            this.lbloiID.Text = model.oiID;
            this.lbloiTime.Text = model.oiTime.ToString();
            this.txtoiCusName.Text = model.oiCusName;
            this.txtoiCusAddress.Text = model.oiCusAddress;
            this.txtOiCusContact.Text = model.OiCusContact;
            this.chkoiIsToTask.Checked = model.oiIsToTask;
            this.lbloiCrtTime.Text = model.oiCrtTime.ToString();
            this.txtoIDelivTime.Text = model.oIDelivTime < Convert.ToDateTime("1987-7-7") ? "" : model.oIDelivTime.ToString("yyyy-MM-dd");
            this.txtoiFiID.Text = model.oiFiID;
            this.txtoiMeasure.Text = model.oiMeasure;
            this.txtoiAddvance.Text = model.oiAddvance.ToString();
            this.txtoiContractFee.Text = model.oiContractFee.ToString();
            this.txtoiSettleFee.Text = model.oiSettleFee.ToString();
            this.lbloiAccessary.Text = model.oiAccessary;
            if (model.oiAccessary == "")
            {
                this.hploiAccessary.Text = "没有附件";
                this.hploiAccessary.Enabled = false;
            }
            else
            {
                this.hploiAccessary.NavigateUrl = "../UploadFiles/Files/Order/" + model.oiAccessary;
            }
            this.ckboiIsSendToF.Checked = model.oiIsSendToF;
            this.lbloiSendToFTime.Text = model.oiSendToFTime.ToString();
            this.ckboiIsBackToS.Checked = model.oiIsBackToS;
            this.lbloiBackToSTime.Text = model.oiBackToSTime.ToString();
            this.lbloiBackResult.Text = model.oiBackResult.ToString();
            IMIS_ERP.BLL.Shop_Info bll1 = new IMIS_ERP.BLL.Shop_Info();
            IMIS_ERP.Model.Shop_Info model1 = bll1.GetModel(Session["siID"].ToString());
            if (model.oiIsSendToF)
            {
                btnAdd.Enabled = false;
                btnCreate.Enabled = false;
                btnSave.Enabled = false;
            }
            //if (model.oiIsCancel)
            //{
            //    btnCreate.Enabled = false;
            //}

        }

        /// <summary>单击按钮事件：保存
        ///     <remarks>2010-12-12 河大信管团队</remarks>
        /// </summary>
        public void btnSave_Click(object sender, EventArgs e)
        {
            if (!this.IsValid)
            {
                return;
            }
            string fileName = lbloiID.Text.Trim();
            if (fuloiAccessary.HasFile)
            {
                string FileType = fuloiAccessary.PostedFile.FileName.Substring(fuloiAccessary.PostedFile.FileName.LastIndexOf("."));
                if (FileType.ToLower() != ".jpg" && FileType.ToLower() != ".gif" && FileType.ToLower() != ".bmp" && FileType.ToLower() != ".png" && FileType.ToLower() != ".doc" && FileType.ToLower() != ".xls" && FileType.ToLower() != ".rar" && FileType.ToLower() != ".zip")
                {
                    lblMessage.Text = "请确认上传的附件格式为.jpg|.gif|.bmp|.png|.doc|.xls|.rar|.zip。";
                    return;
                }
                try
                {
                    fileName += fuloiAccessary.PostedFile.FileName.Substring(fuloiAccessary.PostedFile.FileName.LastIndexOf("."));
                    fuloiAccessary.PostedFile.SaveAs(Server.MapPath("..") + "\\UploadFiles\\Files\\Order\\" + fileName);
                }
                catch
                {
                    lblMessage.Text = "附件上传出错，请重试！";
                    return;
                }
            }
            else
            {
                fileName = lbloiAccessary.Text.Trim();
            }

            IMIS_ERP.Model.Order_Info model = new IMIS_ERP.Model.Order_Info();
            model.oiID = this.lbloiID.Text.Trim();
            model.oiTime = Convert.ToDateTime(this.lbloiTime.Text.Trim());
            model.oiCusName = txtoiCusName.Text.Trim();
            model.oiCusAddress = txtoiCusAddress.Text.Trim();
            model.OiCusContact = txtOiCusContact.Text.Trim();
            model.oIDelivTime = DateTime.Parse(this.txtoIDelivTime.Text.Trim());
            model.oiMeasure = this.txtoiMeasure.Text.Trim();
            model.oiAddvance = decimal.Parse(this.txtoiAddvance.Text.Trim() == "" ? "0" : this.txtoiAddvance.Text.Trim());
            model.oiContractFee = decimal.Parse(this.txtoiContractFee.Text.Trim() == "" ? "0" : this.txtoiContractFee.Text.Trim());
            model.oiSettleFee = decimal.Parse(this.txtoiSettleFee.Text.Trim() == "" ? "0" : this.txtoiSettleFee.Text.Trim());
            model.oiAccessary = fileName;
            model.oiFiID = txtoiFiID.Text.Trim();
            IMIS_ERP.BLL.Order_Info bll = new IMIS_ERP.BLL.Order_Info();
            if (bll.Update(model))
            {
                lbloiAccessary.Text = fileName;
                lblMessage.Text = "修改信息成功";
            }
            else
            {
                lblMessage.Text = "修改信息失败,请重试";
            }

        }

        /// <summary>单击按钮事件：提交给加工中心
        ///     <remarks>2010-12-12 河大信管团队</remarks>
        /// </summary>
        protected void btnCreate_Click(object sender, EventArgs e)
        {
            if (!this.IsValid)
            {
                return;
            }
            IMIS_ERP.BLL.Shop_Info bllsi = new IMIS_ERP.BLL.Shop_Info();
            Decimal siFee = Convert.ToDecimal(bllsi.GetsiFee(Session["siID"].ToString()));
            if (siFee < Convert.ToDecimal(txtoiContractFee.Text.Trim()))
            {
                lblMessage.Text = "消费已超过信用额度，请进行充值！";
                return;
            }
            string fileName = lbloiID.Text.Trim();
            if (fuloiAccessary.HasFile)
            {
                string FileType = fuloiAccessary.PostedFile.FileName.Substring(fuloiAccessary.PostedFile.FileName.LastIndexOf("."));
                if (FileType.ToLower() != ".jpg" && FileType.ToLower() != ".gif" && FileType.ToLower() != ".bmp" && FileType.ToLower() != ".png" && FileType.ToLower() != ".doc" && FileType.ToLower() != ".xls" && FileType.ToLower() != ".rar" && FileType.ToLower() != ".zip")
                {
                    lblMessage.Text = "请确认上传的附件格式为.jpg|.gif|.bmp|.png|.doc|.xls|.rar|.zip。";
                    return;
                }
                try
                {
                    fileName += fuloiAccessary.PostedFile.FileName.Substring(fuloiAccessary.PostedFile.FileName.LastIndexOf("."));
                    fuloiAccessary.PostedFile.SaveAs(Server.MapPath("..") + "\\UploadFiles\\Files\\Order\\" + fileName);
                }
                catch
                {
                    lblMessage.Text = "附件上传出错，请重试！";
                    return;
                }
            }
            else
            {
                fileName = lbloiAccessary.Text.Trim();
            }

            IMIS_ERP.Model.Order_Info model = new IMIS_ERP.Model.Order_Info();
            model.oiID = this.lbloiID.Text.Trim();
            model.oiTime = Convert.ToDateTime(this.lbloiTime.Text.Trim());
            model.oiCusName = txtoiCusName.Text.Trim();
            model.oiCusAddress = txtoiCusAddress.Text.Trim();
            model.OiCusContact = txtOiCusContact.Text.Trim();
            model.oIDelivTime = DateTime.Parse(this.txtoIDelivTime.Text.Trim());
            model.oiMeasure = this.txtoiMeasure.Text.Trim();
            model.oiAddvance = decimal.Parse(this.txtoiAddvance.Text.Trim() == "" ? "0" : this.txtoiAddvance.Text.Trim());
            model.oiContractFee = decimal.Parse(this.txtoiContractFee.Text.Trim() == "" ? "0" : this.txtoiContractFee.Text.Trim());
            model.oiSettleFee = decimal.Parse(this.txtoiSettleFee.Text.Trim() == "" ? "0" : this.txtoiSettleFee.Text.Trim());
            model.oiAccessary = fileName;
            model.oiFiID = txtoiFiID.Text.Trim();
            IMIS_ERP.BLL.Order_Info bll = new IMIS_ERP.BLL.Order_Info();
            bll.Update(model);
            IMIS_ERP.BLL.Order_Info bll1 = new IMIS_ERP.BLL.Order_Info();
            if (bll1.SendToF(lbloiID.Text.Trim()))
            {
                IMIS_ERP.Model.Head_Shop model2 = new IMIS_ERP.Model.Head_Shop();
                IMIS_ERP.BLL.Head_Shop bll2 = new IMIS_ERP.BLL.Head_Shop();
                model2.hsTime = DateTime.Now;
                model2.hsShopID = Session["siID"].ToString();
                model2.hsSummary = "订单消费";
                IMIS_ERP.BLL.Shop_Info bll5 = new IMIS_ERP.BLL.Shop_Info();
                IMIS_ERP.Model.Shop_Info model5 = bll5.GetModel(Session["siID"].ToString());
                if (model5.siIsFlagship)
                {
                    model2.hsFee = decimal.Parse(this.txtoiSettleFee.Text.Trim());
                }
                else
                {
                    model2.hsFee = decimal.Parse(this.txtoiContractFee.Text.Trim());
                }
                model2.hsReceived = false;
                model2.hsHuID = Session["UId"].ToString();
                model2.hsVoucher = lbloiID.Text.Trim();
                model2.hsRemark = "订单消费";

                IMIS_ERP.BLL.Shop_Info bll3 = new IMIS_ERP.BLL.Shop_Info();
                model2.hsSum = decimal.Parse(bll3.GetsiFee1(model2.hsShopID, "货款")) - model2.hsFee;
                model2.hsType = "消费";
                bll2.Add(model2);
                bll3.UpdateSiFee(model2.hsShopID, model2.hsSum, "货款");

                lblMessage.Text = "提交给加工中心成功";
                ShowInfo(Request.Params["id"]);
                Set_GV();
            }
            else
            {
                lblMessage.Text = "提交给加工中心失败,请重试";
            }
        }

        /// <summary>单击按钮事件：添加产品
        ///     <remarks>2010-12-12 河大信管团队</remarks>
        /// </summary>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            IMIS_ERP.Model.Order_Products model = new IMIS_ERP.Model.Order_Products();
            model.opOiID = this.lbloiID.Text.Trim();
            model.opPiID = this.ddlopPiID.Text.Trim();
            model.opPiName = this.txtopPiName.Text.Trim();
            model.opWallThick = decimal.Parse(this.ddlopWallThick.SelectedItem.Text.Trim());
            model.opPcID = this.txtopPcID.Text.Trim();
            model.opArea = decimal.Parse(this.txtopArea.Text.Trim());
            model.opRemark = this.txtopRemark.Text.Trim();
            model.opPrice1 = decimal.Parse(this.txtopPrice1.Text.Trim());
            model.opPrice2 = decimal.Parse(this.txtopPrice2.Text.Trim());
            model.opFigure = decimal.Parse(this.txtopFigure.Text.Trim());
            model.opFigureNum = decimal.Parse(this.txtopFigureNum.Text.Trim());
            model.opFigureSum = decimal.Parse(this.txtopFigureSum.Text.Trim());
            model.opLock = decimal.Parse(this.txtopLock.Text.Trim());
            model.opMarkup = decimal.Parse(this.txtopMarkup.Text.Trim());
            model.opDisCount = decimal.Parse(this.txtopDisCount.Text.Trim());
            model.opMarkupOther = decimal.Parse(this.txtopMarkupOther.Text.Trim());
            model.opNum = int.Parse(this.txtopNum.Text.Trim());
            //计算销售金额：销售金额=(销售单价*面积*折扣*(1+加价)+加花总价+加锁价格+其他加价)*数量
            model.opFee2 = (model.opPrice2 * model.opArea * model.opDisCount * (1 + model.opMarkup) + model.opFigureSum + model.opLock + model.opMarkupOther) * Convert.ToDecimal(model.opNum);
            //计算结算金额：结算金额=销售金额-(销售单价-结算单价)*面积*数量
            model.opFee1 = model.opFee2 - (model.opPrice2 - model.opPrice1) * model.opArea * Convert.ToDecimal(model.opNum);
            //加工费已经乘了数量
            model.opMachineFee = Convert.ToDecimal(ViewState["pcPrice"]) * model.opArea * Convert.ToDecimal(model.opNum);
            model.opWeightPerKg = Convert.ToDecimal(ViewState["pcWeightPerSquare"]);
            //总重量没有乘以数量
            model.opWeight = model.opWeightPerKg * model.opArea;
            //安装费已经乘了数量
            model.opAreaPerInstall = Convert.ToDecimal(ViewState["pcFeeInstall"]);
            model.opInstallFee = Convert.ToDecimal(ViewState["pcFeeInstall"]) * model.opArea * Convert.ToDecimal(model.opNum);
            //代理商结算已经乘了数量
            model.opAreaPerAgent = Convert.ToDecimal(ViewState["pcFeeAgent"]);
            model.opAgentFee = Convert.ToDecimal(ViewState["pcFeeAgent"]) * model.opArea * Convert.ToDecimal(model.opNum);
            IMIS_ERP.BLL.Order_Products bll = new IMIS_ERP.BLL.Order_Products();
            //UP_Order_Products_ADD
            if (bll.Add(model))
            {
                lblMessage2.Text = "添加信息成功";
                Bind_GV();
                ShowInfo(Request.Params["id"]);
                txtopFigureNum.Text = "0";
                txtopFigureSum.Text = "0";
                txtopMarkup.Text = "0.0";
                txtopMarkupOther.Text = "0";
                txtopArea.Text = "";
                txtopNum.Text = "1";
                txtopRemark.Text = "";
            }
            else
            {
                lblMessage2.Text = "添加信息失败,请重试";
            }
        }

        /// <summary>绑定数据：GridView
        ///     <remarks>2010-12-08 河北大学信管团队</remarks>
        /// </summary>
        public void Bind_GV()
        {
            IMIS_ERP.BLL.Order_Products bll = new IMIS_ERP.BLL.Order_Products();
            SqlDataReader dr = bll.GetList(lbloiID.Text.Trim());
            //为GridView绑定赋予数据源
            GV.DataSource = dr;
            //为GridView绑定数据
            GV.DataBind();
            dr.Close();
        }

        /// <summary>响应GridView事件：创建数据行时
        /// 	<remark>2010-12-08 BY 河北大学信管团队</remark>
        /// </summary>
        protected void GV_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //为数据行增加鼠标移动到行特性
                e.Row.Attributes.Add("onmouseover", "c=style.backgroundColor;this.style.backgroundColor='" + IMIS_ERP.BLL.Assistant.getColor1 + "'");
                //为数据行增加鼠标离开行特性
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            }
        }

        /// <summary>响应GridView事件：删除记录时
        /// 	<remark>2010-12-03 BY 河北大学信管团队</remark>
        /// </summary>
        protected void GV_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            IMIS_ERP.BLL.Order_Products bll = new IMIS_ERP.BLL.Order_Products();
            if (bll.Delete(GV.DataKeys[e.RowIndex][0].ToString()))
            {
                lblMessage2.Text = "删除数据成功";
                Bind_GV();
                ShowInfo(Request.Params["id"]);
            }
            else
            {
                lblMessage2.Text = "删除数据失败";
            }
        }

        /// <summary>下拉框切换事件：产品编号
        /// 	<remark>2010-12-12 BY 河北大学信管团队</remark>
        /// </summary>
        protected void ddlopPiID_SelectedIndexChanged(object sender, EventArgs e)
        {
            Bind_ddlopWallThick();
        }

        /// <summary>下拉框切换事件：壁厚
        /// 	<remark>2010-12-17 BY 河北大学信管团队</remark>
        /// </summary>
        protected void ddlopWallThick_SelectedIndexChanged(object sender, EventArgs e)
        {
            Bind_Price();
        }

        /// <summary>下拉框切换事件：加锁
        /// 	<remark>2010-12-17 BY 河北大学信管团队</remark>
        /// </summary>
        protected void ddlopLock_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtopLock.Text = ddlopLock.SelectedValue;
        }

        /// <summary>
        /// 文本框改变事件：加花
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void txtopFigureNum_TextChanged(object sender, EventArgs e)
        {
            try
            {
                txtopFigureSum.Text = Convert.ToString(Convert.ToDecimal(txtopFigure.Text.Trim()) * Convert.ToDecimal(txtopFigureNum.Text.Trim()));
            }
            catch
            {
                lblMessage2.Text = "请输入正确的数量！";
            }
        }
    }
}